package com.lxhll.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxhll.personnel.core.P;
import com.lxhll.personnel.core.ResponseData;
import com.lxhll.personnel.entity.Dictionary;
import com.lxhll.personnel.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * (Dictionary)表控制层
 *
 * @author makejava
 * @since 2021-07-18 18:29:15
 */
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class DictionaryController {
    /**
     * 服务对象
     */
    @Autowired
    private DictionaryService dictionaryService;


    /**
     *  获取全部数据，供管理员增删改查
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody Map<String ,Object> params){
//        获取全部数据，供管理员增删改查
        //提取查询参数与分页
        log.info("查询参数:"+params);
        //构建wrapper
        QueryWrapper<Dictionary> wrapper=new QueryWrapper<>();
        //查询字典列表,可以按照字典类型，字典名称
        Integer id=(Integer)params.get("id");
        String name=(String)params.get("key");
        String type=(String)params.get("value");
        if(id!=null)
            wrapper.like("id",id);
        if(StringUtils.isNotBlank(name))
            wrapper.like("key",name);
        if(StringUtils.isNotBlank(type))
            wrapper.like("value",type);

        IPage<Dictionary> dictionaryIPage=dictionaryService.page(P.page(params),wrapper);
        //返回
        return ResponseData.ok(dictionaryIPage);
    }

    /**
     * 查询字典表的多个类型下拉框,根据下拉框要的类型返回
     */
    @PostMapping("/droplist")
    public ResponseData droplist(@RequestBody String[] types){
        Map<String ,Object> map=new HashMap<>();
        QueryWrapper<Dictionary> wrapper=new QueryWrapper<>();
        for(String key:types){
            map.put(key,dictionaryService.list(wrapper.eq("`key`",key)));
            wrapper.clear();
        }
        return ResponseData.ok(map);
    }

    /**
     * 查询单个字典信息
     * @param
     * @return
     */
    @GetMapping("/info/{id}")
    public ResponseData info(@PathVariable("id") Integer id){
        Dictionary Dictionary = dictionaryService.getById(id);
        return ResponseData.ok(Dictionary);
    }

    /**
     * 删除下拉框，可以批量
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseData deleteDictionary(@RequestBody Integer[] ids){
        log.info("删除下拉框id"+ Arrays.toString(ids));
        boolean res=dictionaryService.removeByIds(Arrays.asList(ids));
        if(!res){
            return ResponseData.error("删除失败");
        }
        return ResponseData.ok();
    }

    /**
     * 下拉框信息修改
     * @param Dictionary
     * @return
     */
    @PutMapping(value="/update")
    public ResponseData update(@Validated @RequestBody Dictionary Dictionary) {
        boolean res= dictionaryService.updateById(Dictionary);
        if(!res){
            return ResponseData.error("修改失败");
        }
        return ResponseData.ok();
    }

    /**
     * 增加下拉框
     */
    @PostMapping("/save")
    public ResponseData add(@Validated @RequestBody Dictionary Dictionary){
        boolean res=dictionaryService.save(Dictionary);
        if(!res){
            return ResponseData.error("添加失败");
        }
        return ResponseData.ok();
    }
}
